/*
    jME Task Framework, for executing tasks in a Java/OpenGL enviroment
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.task;

public abstract class Task implements Comparable {
		
	private transient TaskRunner taskrunner = null;
	private transient TaskManager taskmanager = null;
	
	protected void assign(int id) {
		taskrunner.assign(this, id);
	}
	protected void prepareRun() {
		taskrunner.prepareRun(this);		
	}
	
	protected void resume() {
		taskrunner.resume(this);
	}
	protected void attemptPause() {
		if (taskrunner.isPausable()) {
			taskrunner.pause(this);
		}
	}
	
	protected void reset() {
		taskrunner.removeTask(this);
		taskrunner = null;
		taskmanager = null;
		completed = false;
	}

	
	protected boolean isPaused() {
		return taskrunner.isPaused(this);
	}
	
	protected boolean isUsingManager(TaskManager manager) {
		return (manager == this.taskmanager); 
	}
	
	protected boolean isUsingRunner(TaskRunner runner) {
		return (runner == this.taskrunner); 
	}
	
	protected synchronized void setManagerAndRunner(TaskManager manager, TaskRunner runner) {
		if (taskmanager == null) {
			this.taskmanager = manager;
			this.taskrunner = runner;
		}
		else
			throw new TaskException("This task is already assigned to a task manager");
	}
	
	protected boolean isCompleted() {
		return completed;
	}
	
	private boolean completed = false;
	
	public void complete() {
		completed = true;
	}
	
	protected long priority = 0;
	/**
	 * Sets the priority for this task, lower value means more urgency
	 * @param priority
	 * 				the priority value
	 */
	public void setPriority(long priority) {
		this.priority = priority;
	}	
	public abstract void updatePriority();
	
	/**
	 * Implement the work your task should do here. This method should 
	 * finish when the desired amount of work for this cycle is done. Returned
	 * is wether the work of this task is done, or if more calls to runTask() are required.
	 * 
	 * @return wether the work part of this task is finished.
	 */
	public abstract boolean runTask();
	
	protected boolean runTaskFromRunner() {
		completed = runTask();
		return completed; 
	}
	
	public abstract Object identifier();
	
	/**
	 * sort in reverse order, since lower values should be executed first, 
	 * and execution starts from the top of the stack.
	 */
	public int compareTo(Object task) {
		long taskprio = ((Task)task).priority;
		if (taskprio < priority)
			return -1;
		if (taskprio > priority)
			return 1;
		return 0;
	}
	
}
